package com;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        // 定义 API 的 URL 和端点
        String apiUrl = "http://127.0.0.1:7861/api/voice_creation";

        // 构造请求体 JSON 数据
        String jsonBody = """
            {
                "data": [
                    "这是一段测试文本。",
                    "female",
                    4,
                    2
                ]
            }
        """;

        // 创建 HttpClient 实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建 HttpRequest 实例
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(apiUrl))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        try {
            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 打印响应状态码和响应体
            System.out.println("Response Code: " + response.statusCode());
            System.out.println("Response Body: " + response.body());

            // 解析响应中的音频文件路径
            if (response.statusCode() == 200) {
                String responseBody = response.body();
                if (responseBody.contains("/path/to/generated/audio.wav")) {
                    System.out.println("Generated Audio Path: /path/to/generated/audio.wav");
                } else {
                    System.out.println("Failed to parse audio path.");
                }
            } else {
                System.out.println("Error: Received non-200 status code.");
            }

        } catch (IOException | InterruptedException e) {
            // 捕获异常并打印错误信息
            System.err.println("Error occurred while sending the request: " + e.getMessage());
        }
    }
}